using System;
using Gtk;
using MonoDevelop.ResourceEditor;

namespace MonoDevelop.ResourceEditor.Gui
{
	public class StringView : Gtk.TextView, IResourceView
	{
		#region Events
		/// <summary>Event is raised when the value of the resource has been changed</summary>
		public event ResourceChangedEventHandler ResourceChanged;
		#endregion
		
		#region Properties
		/// <summary>Gets or sets whether or not the current view is write protected or not</summary>
		/// <value>True if the widget is write protected, otherwise flase</value>
		public bool WriteProtected
		{
			get { return !this.Editable; }
			set { this.Editable = !value; }
		}
		
		/// <summary>Gets or sets the resource item for the widget</summary>
		/// <value>A ResourceItem containing the name and value for the resource</value>
		public ResourceItem ResourceItem
		{
			get { return resourceItem; }
			set 
			{
				resourceItem = value;
				this.Buffer.Text = (string)value.ResourceValue;
			}
		}
		#endregion
		
		#region Member Variables
		/// <summary>Holds the value for the ResourceItem property</summary>
		private ResourceItem resourceItem;
		#endregion
		
		/// <summary>Initializes a new instance of the StringView widget by specifying the ResourceItem</summary>
		/// <param name="item">A ResourceItem containing the value to display</param> 
		public StringView(ResourceItem item)
		{
			this.ResourceItem = item;
			this.Buffer.Changed += new EventHandler(BufferText_Changed);
		}
		
		/// <summary>Raise the event when the resource value has been changed</summary>
		/// <param name="resourceName">A string containing the name of the resource</param>
		/// <param name="val">An object containing the value for the resource</param>
		protected void OnResourceChanged(string resourceName, object val)
		{
			if(ResourceChanged != null) 
			{
				ResourceChanged(this, new ResourceEventArgs(resourceName, val));
			}
		}
		
		/// <summary>Execute the event when the resource value has been changed</summary>
		/// <param name="sender">An object that contains the sender data.</param>
		/// <param name="e">An EventArgs object that contains the event data.</param>
		void BufferText_Changed(object sender, EventArgs e)
		{
			if(ResourceChanged != null) 
			{
				ResourceChanged(this, new ResourceEventArgs(resourceItem.Name, this.Buffer.Text));
			}
		}
		
	}
	
}
